﻿@model ProductReviewListModel
@inject AdminAreaSettings adminAreaSettings
@{
    //page title
    ViewBag.Title = Loc["Admin.Catalog.ProductReviews"];
}

<div class="row">
    <div class="col-md-12">
        <div class="x_panel light form-fit popup-window">
            <div class="x_title">
                <div class="caption">
                    <i class="fa fa-cubes"></i>
                    @Loc["Admin.Catalog.ProductReviews"]
                </div>
                <div class="actions btn-group btn-group-devided">
                    <input type="button" id="approve-selected" class="btn default" value="@Loc["Admin.Catalog.ProductReviews.ApproveSelected"]"/>
                    <input type="button" id="disapprove-selected" class="btn default" value="@Loc["Admin.Catalog.ProductReviews.DisapproveSelected"]"/>
                    <vc:admin-widget widget-zone="product_review_list_buttons" additional-data="null"/>
                </div>
            </div>

            <div class="x_content form">
                <div class="form-horizontal">
                    <div class="form-body">
                        <div class="main-header col-12 px-0">
                            <div class="row align-items-end">
                                <div class="col-md-4 col-ms-12 col-12">
                                    <div class="form-group mb-0">
                                        <admin-label asp-for="SearchProductId" class="control-label"/>
                                        <div>
                                            <input type="text" class="form-control text-box single-line" id="search-product-name" autocomplete="off"/>
                                            <span id="search-product-friendly-name"></span>
                                            <input type="button" id="search-product-clear" class="k-button" style="display: none;" value="@Loc["Admin.Common.Clear"]"/>
                                            <admin-textarea asp-for="SearchProductId" autocomplete="off" style="display: none;"></admin-textarea>
                                            <script>
                                                $(document).ready(function() {
                                                    $('#search-product-name').autocomplete({
                                                        delay: 500,
                                                        minLength: 3,
                                                        source: '@(Url.Action("ProductSearchAutoComplete", new { area = Constants.AreaAdmin }))',
                                                        select: function(event, ui) {
                                                            $('#@Html.IdFor(model => model.SearchProductId)').val(ui.item.productid);
                                                            $('#search-product-friendly-name').text(ui.item.label);
                                                            $('#search-product-clear').show();
                                                            return false;
                                                        }
                                                    });

                                                    //remove button
                                                    $('#search-product-clear').click(function() {
                                                        $('#@Html.IdFor(model => model.SearchProductId)').val('');
                                                        $('#search-product-friendly-name').text('');
                                                        $('#search-product-clear').hide();
                                                        return false;
                                                    });
                                                });
                                            </script>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4 col-ms-12 col-12">
                                    <div class="form-group mb-0">
                                        <admin-label asp-for="SearchText" class="control-label"/>
                                        <admin-input asp-for="SearchText"/>
                                    </div>
                                </div>
                                <div class="col-md-4 col-sm-12 col-12">
                                    <div class="form-actions">
                                        <div class="btn-group">
                                            <button class="btn btn-success filter-submit" id="search-productreviews">
                                                <i class="fa fa-search"></i> @Loc["Admin.Common.Search"]
                                            </button>
                                            <button class="btn btn-info" type="button" data-toggle="collapse" data-target="#filterCollapse" aria-expanded="false" aria-controls="filterCollapse">
                                                <i class="fa fa-filter"></i>&nbsp; @Loc["Admin.Common.Filters"]
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="collapse" id="filterCollapse">
                            <div class="drop-filters-container">
                                <div class="form-group">
                                    <admin-label asp-for="CreatedOnFrom" class="col-sm-3 control-label"/>
                                    <div class="col-md-9 col-sm-9">
                                        <admin-input asp-for="CreatedOnFrom"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <admin-label asp-for="CreatedOnTo" class="col-sm-3 control-label"/>
                                    <div class="col-md-9 col-sm-9">
                                        <admin-input asp-for="CreatedOnTo"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <admin-label asp-for="SearchStoreId" class="col-sm-3 control-label"/>
                                    <div class="col-md-9 col-sm-9">
                                        <admin-select asp-for="SearchStoreId" asp-items="Model.AvailableStores"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="x_content">
                        <div id="productreviews-grid"></div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

<script>
$(document).ready(function () {
    $("#productreviews-grid").kendoGrid({
        dataSource: {
            transport: {
                read: {
                    url: "@Html.Raw(Url.Action("List", "ProductReview", new { area = Constants.AreaAdmin }))",
                    type: "POST",
                    dataType: "json",
                    data: additionalData
                }
            },
            schema: {
                data: "Data",
                total: "Total",
                errors: "Errors"
            },
            error: function(e) {
                display_kendoui_grid_error(e);
                // Cancel the changes
                this.cancelChanges();
            },
            pageSize: @(adminAreaSettings.DefaultGridPageSize),
            serverPaging: true,
            serverFiltering: true,
            serverSorting: true
        },
        pageable: {
            refresh: true,
            pageSizes: [@(adminAreaSettings.GridPageSizes)]
        },
        scrollable: false,
        dataBound: onDataBound,
        columns: [{
            field: "Ids",
            headerTemplate: "<label class='mt-checkbox mt-checkbox-outline control control-checkbox'><input id='mastercheckbox' type='checkbox'/> <div class='control__indicator'></div></label>",
            headerAttributes: { style: "text-align:center" },
            attributes: { style: "text-align:center" },
            template: "<label class='mt-checkbox mt-checkbox-outline control control-checkbox'><input type='checkbox' value='#=Ids#' class='checkboxGroups'/> <div class='control__indicator'></div></label>",
            width: 40
        },
        {
            field: "ProductName",
            title: "@Loc["Admin.Catalog.ProductReviews.Fields.Product"]",
            width: 200,
            template: '<a class="k-link" href="@Url.Action("Edit", "Product", new { area = Constants.AreaAdmin })/#=ProductId#">#:kendo.htmlEncode(ProductName)#</a>'
        }, {
            field: "CustomerId",
            title: "@Loc["Admin.Catalog.ProductReviews.Fields.Customer"]",
            width: 200,
            minScreenWidth: 750,
            template: '<a class="k-link" href="@Url.Action("Edit", "Customer", new { area = Constants.AreaAdmin })/#=CustomerId#">#:kendo.htmlEncode(CustomerInfo)#</a>'
        }, {
            field: "Title",
            title: "@Loc["Admin.Catalog.ProductReviews.Fields.Title"]",
            width: 200,
            template: '<a class="k-link" href="Edit/#=Id#?ProductId=#=ProductId#">#:kendo.htmlEncode(Title)#</a>'
        }, {
            field: "Rating",
            title: "@Loc["Admin.Catalog.ProductReviews.Fields.Rating"]",
            width: 60,
            minScreenWidth: 750,
        }, {
            field: "IsApproved",
            title: "@Loc["Admin.Catalog.ProductReviews.Fields.IsApproved"]",
            width: 80,
            headerAttributes: { style: "text-align:center" },
            attributes: { style: "text-align:center" },
            template: '# if(IsApproved) {# <i class="fa fa-check" aria-hidden="true" style="color:green"></i> #} else {# <i class="fa fa-times" aria-hidden="true" style="color:red"></i> #} #'
        }, {
            field: "CreatedOn",
            title: "@Loc["Admin.Catalog.ProductReviews.Fields.CreatedOn"]",
            width: 120,
            type: "date",
            format: "{0:G}"
        }]
    });
});
</script>
<script>

    var selectedIds = [];

    $(document).ready(function () {

        //search button
        $('#search-productreviews').click(function () {
            //search
            var grid = $('#productreviews-grid').data('kendoGrid');
            grid.dataSource.page(1); //new search. Set page size to 1
            //grid.dataSource.read(); we already loaded the grid above using "page" function
            //clear selected checkboxes
            $('.checkboxGroups').prop('checked', false).change();
            selectedIds = [];
            return false;
        });

        //"approve selected" button
        $('#approve-selected').click(function (e) {
            e.preventDefault();

            var postData = {
                selectedIds: selectedIds,
            };
            addAntiForgeryToken(postData);

            $.ajax({
                cache:false,
                type: "POST",
                url: "@(Url.Action("ApproveSelected", "ProductReview", new { area = Constants.AreaAdmin }))",
                data: postData,
                complete: function (data) {
                    //reload grid
                    var grid = $('#productreviews-grid').data('kendoGrid');
                    grid.dataSource.read();
                },
                error:function (xhr, ajaxOptions, thrownError){
                    alert(thrownError);
                },
                traditional: true
            });
            return false;
        });

        //"disapprove selected" button
        $('#disapprove-selected').click(function (e) {
            e.preventDefault();

            var postData = {
                selectedIds: selectedIds,
            };
            addAntiForgeryToken(postData);

            $.ajax({
                cache:false,
                type: "POST",
                url: "@(Url.Action("DisapproveSelected", "ProductReview", new { area = Constants.AreaAdmin }))",
                data: postData,
                complete: function (data) {
                    //reload grid
                    var grid = $('#productreviews-grid').data('kendoGrid');
                    grid.dataSource.read();
                },
                error:function (xhr, ajaxOptions, thrownError){
                    alert(thrownError);
                },
                traditional: true
            });
            return false;
        });


        $('#mastercheckbox').click(function () {
            $('.checkboxGroups').prop('checked', $(this).is(':checked')).change();
        });

        //wire up checkboxes.
        $('#productreviews-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function (e) {
            var $check = $(this);
            if ($check.is(":checked") == true) {
                var checked = jQuery.inArray($check.val(), selectedIds);
                if (checked == -1) {
                    //add id to selectedIds.
                    selectedIds.push($check.val());
                }
            }
            else {
                var checked = jQuery.inArray($check.val(), selectedIds);
                if (checked > -1) {
                    //remove id from selectedIds.
                    selectedIds = $.grep(selectedIds, function (item, index) {
                        return item != $check.val();
                    });
                }
            }
            updateMasterCheckbox();
        });
    });

    function additionalData() {
        var data = {
            CreatedOnFrom: $('#@Html.IdFor(model => model.CreatedOnFrom)').val(),
            CreatedOnTo: $('#@Html.IdFor(model => model.CreatedOnTo)').val(),
            SearchText: $('#@Html.IdFor(model => model.SearchText)').val(),
            SearchStoreId: $('#@Html.IdFor(model => model.SearchStoreId)').val(),
            SearchProductId: $('#@Html.IdFor(model => model.SearchProductId)').val()
        };
        addAntiForgeryToken(data);
        return data;
    }

    function onDataBound(e) {

        $('#productreviews-grid input[type=checkbox][id!=mastercheckbox]').each(function () {
            var currentId = $(this).val();
            var checked = jQuery.inArray(currentId, selectedIds);
            //set checked based on if current checkbox's value is in selectedIds.
            $(this).prop('checked', checked > -1);
        });

        updateMasterCheckbox();
    }

    function updateMasterCheckbox() {
        var numChkBoxes = $('#productreviews-grid input[type=checkbox][id!=mastercheckbox]').length;
        var numChkBoxesChecked = $('#productreviews-grid input[type=checkbox][id!=mastercheckbox]:checked').length;
        $('#mastercheckbox').prop('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
    }

</script>